using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DBSourceToolsLib.UI.Forms
{
    public partial class TableOptionsForm : Form
    {
        private string m_strDatabaseName;
        private string m_strScriptOptionsXmlFile;
        // TODO : retain existing selections on refresh

        public TableOptionsForm(string strSourceDatabaseName, string strScriptOptionsXmlFile)
        {
            InitializeComponent();
            this.m_strDatabaseName = strSourceDatabaseName;
            this.m_strScriptOptionsXmlFile = strScriptOptionsXmlFile;

            if (System.IO.File.Exists(strScriptOptionsXmlFile))
            {
                this.m_scriptOptionsDS.ReadXml(strScriptOptionsXmlFile);
                this.m_scriptOptionsDS.ScriptOptions.AcceptChanges();
            }
            this.m_dbListView.DataSet = this.m_scriptOptionsDS;
            this.m_dbListView.AddColumn("TableName", DBSourceToolsLib.SysObjects.UI.DBSourceListViewColType.Text);
            this.m_dbListView.AddColumn("ScriptTable", DBSourceToolsLib.SysObjects.UI.DBSourceListViewColType.Checkbox);
            this.m_dbListView.AddColumn("ScriptData", DBSourceToolsLib.SysObjects.UI.DBSourceListViewColType.Checkbox);

            this.m_dbListView.Bind();

        }
        
        private void btnLoadFromDB_Click(object sender, EventArgs e)
        {

        }


        private void TableOptionsForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.m_scriptOptionsDS.HasChanges())
            {
                if (MessageBox.Show(this, "Save Changes ?", "Table Options", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    FileUtils.EnsureFileIsWriteable(m_strScriptOptionsXmlFile);
                    this.m_scriptOptionsDS.WriteXml(this.m_strScriptOptionsXmlFile);
                }
            }
        }

        private void m_toolStripButtonReload_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;

            SourceDatabase oSourceDatabase
                = DBSourceToolsLib.UserSettings.Instance().getSourceDatabase(this.m_strDatabaseName);

            DBSourceToolsLib.ScriptOptionsLoader oLoader = new ScriptOptionsLoader(this,
                DBSourceToolsLib.Commands.DatabaseCommandTypes.LoadTableList);
            oLoader.Execute(ref oSourceDatabase, null);

//            oDBLoadDb.Execute(ref oSourceDatabase, false);

            this.m_scriptOptionsDS = oLoader.TableScriptOptionsDS;
            this.m_dbListView.DataSet = this.m_scriptOptionsDS;
            this.m_dbListView.Bind();

            this.Cursor = Cursors.Default;

        }

        private void m_toolStripButtonSave_Click(object sender, EventArgs e)
        {
            this.m_scriptOptionsDS.AcceptChanges();
            FileUtils.EnsureFileIsWriteable(this.m_strScriptOptionsXmlFile);
            this.m_scriptOptionsDS.WriteXml(this.m_strScriptOptionsXmlFile);
        }


        private void scriptNoneToolStripMenuItemTables_Click(object sender, EventArgs e)
        {
            foreach (DataRow oRow in this.m_scriptOptionsDS.Tables[0].Rows)
            {
                oRow["ScriptTable"] = false;
            }
            this.m_dbListView.Invalidate();
        }

        private void scriptAllToolStripMenuItemData_Click(object sender, EventArgs e)
        {
            foreach (DataRow oRow in this.m_scriptOptionsDS.Tables[0].Rows)
            {
                oRow["ScriptData"] = true;
            }
            this.m_dbListView.Invalidate();
        }

        private void scriptNoneToolStripMenuItemData_Click(object sender, EventArgs e)
        {
            foreach (DataRow oRow in this.m_scriptOptionsDS.Tables[0].Rows)
            {
                oRow["ScriptData"] = false;
            }
            this.m_dbListView.Invalidate();

        }

        private void mscriptAllToolStripMenuItemTables_Click(object sender, EventArgs e)
        {
            foreach (DataRow oRow in this.m_scriptOptionsDS.Tables[0].Rows)
            {
                oRow["ScriptTable"] = true;
            }
            this.m_dbListView.Invalidate();
        }

        private void selectUsingFilterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Tables | select using filter.
            SysObjects.UI.Forms.SelectFilterForm oSelectFilterForm = new DBSourceToolsLib.SysObjects.UI.Forms.SelectFilterForm();
            if (oSelectFilterForm.ShowDialog(this) == DialogResult.OK)
            {
                string strFilterString = oSelectFilterForm.SelectionFilter;
                if (!String.IsNullOrEmpty(strFilterString))
                {
                    foreach (DataRow oRow in this.m_scriptOptionsDS.Tables[0].Rows)
                    {
                        if (oRow["TableName"].ToString().IndexOf(strFilterString) == 0)
                        {
                            oRow["ScriptTable"] = true;
                        }
                    }
                }
            }
            this.m_dbListView.Invalidate();


        }

        private void selectUsingFilterToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            // Data | select using filter
            SysObjects.UI.Forms.SelectFilterForm oSelectFilterForm = new DBSourceToolsLib.SysObjects.UI.Forms.SelectFilterForm();
            if (oSelectFilterForm.ShowDialog(this) == DialogResult.OK)
            {
                string strFilterString = oSelectFilterForm.SelectionFilter;
                if (!String.IsNullOrEmpty(strFilterString))
                {
                    foreach (DataRow oRow in this.m_scriptOptionsDS.Tables[0].Rows)
                    {
                        if (oRow["TableName"].ToString().IndexOf(strFilterString) == 0)
                        {
                            oRow["ScriptData"] = true;
                        }
                    }
                }
            }
            this.m_dbListView.Invalidate();

        }

    }
}